package com.tim.android.widget.tab;

import java.util.ArrayList;

import com.tim.android.utils.ComputeTool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class iTab extends View {
	private Paint mPaint;// background paint
	private Paint mActiveTextPaint;
	private Paint mInactiveTextPaint;
	private ArrayList<TabMember> mTabMembers;
	private int mActiveTab;
	private OnTabClickListener mOnTabClickListener = null;
	private float textSize;

	public iTab(Context context, AttributeSet attrs) {
		super(context, attrs);
		textSize = ComputeTool.px2dip(context, 24);
		mTabMembers = new ArrayList<TabMember>();
		mPaint = new Paint();
		mActiveTextPaint = new Paint();
		mInactiveTextPaint = new Paint();
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(0xFFFFFF00);
		mPaint.setAntiAlias(true);
		mActiveTextPaint.setTextAlign(Align.CENTER);
		mActiveTextPaint.setTextSize(textSize);
		mActiveTextPaint.setColor(0xFFFFFFFF);
		mActiveTextPaint.setAntiAlias(true);
		mInactiveTextPaint.setTextAlign(Align.CENTER);
		mInactiveTextPaint.setTextSize(textSize);
		mInactiveTextPaint.setColor(0xFF999999);
		mInactiveTextPaint.setAntiAlias(true);
		mActiveTab = 0;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Rect r = new Rect();
		this.getDrawingRect(r);
		// Compute each tab's width.
		int singleTabWidth = r.right
				/ (mTabMembers.size() != 0 ? mTabMembers.size() : 1);
		// Render background.
		canvas.drawColor(0xFF000000);
		mPaint.setColor(0xFF434343);
		canvas.drawLine(r.left, r.top + 1, r.right, r.top + 1, mPaint);
		int color = 62;
		for (int i = 0; i < 40; i++) {
			mPaint.setARGB(255, color, color, color);
			canvas.drawRect(r.left, r.top + i + 1, r.right, r.top + i + 2,
					mPaint);
			color--;
		}
		// Render each tab
		for (int i = 0; i < mTabMembers.size(); i++) {
			TabMember tabMember = mTabMembers.get(i);
			Bitmap icon = null;
			
			// Selected tab
			if (mActiveTab == i) {
				icon = BitmapFactory.decodeResource(getResources(),
						tabMember.selectedResId);
				// Compute img's position of tab.
				int tabImgX = singleTabWidth * i
						+ (singleTabWidth / 2 - icon.getWidth() / 2);
				mPaint.setARGB(37, 255, 255, 255);
				canvas.drawRoundRect(new RectF(r.left + singleTabWidth * i + 3,
						r.top + 3, r.left + singleTabWidth * (i + 1) - 3,
						r.bottom - 2), 5, 5, mPaint);
				canvas.drawBitmap(icon, tabImgX, r.top + 5, mActiveTextPaint);
				canvas.drawText(tabMember.text, singleTabWidth * i
						+ (singleTabWidth / 2), r.bottom - 2, mActiveTextPaint);
			} 
			// Unselected tab
			else {
				icon = BitmapFactory.decodeResource(getResources(),
						tabMember.unselectedResId);
				// Compute img's position of tab.
				int tabImgX = singleTabWidth * i
						+ (singleTabWidth / 2 - icon.getWidth() / 2);
				canvas.drawBitmap(icon, tabImgX, r.top + 5, mInactiveTextPaint);
				canvas.drawText(tabMember.text, singleTabWidth * i
						+ (singleTabWidth / 2), r.bottom - 2,
						mInactiveTextPaint);
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent motionEvent) {
		Rect r = new Rect();
		this.getDrawingRect(r);
		float singleTabWidth = r.right
				/ (mTabMembers.size() != 0 ? mTabMembers.size() : 1);
		int pressedTab = (int) ((motionEvent.getX() / singleTabWidth) - (motionEvent
				.getX() / singleTabWidth) % 1);
		mActiveTab = pressedTab;
		if (this.mOnTabClickListener != null) {
			this.mOnTabClickListener.onTabClick(mTabMembers.get(pressedTab));
		}
		this.invalidate();
		return super.onTouchEvent(motionEvent);
	}

	public void addTabMember(TabMember tabMember) {
		mTabMembers.add(tabMember);
	}

	public void setOnTabClickListener(OnTabClickListener onTabClickListener) {
		mOnTabClickListener = onTabClickListener;
	}

	public static class TabMember {
		public int id;
		public String text;
		public int unselectedResId;
		public int selectedResId;
		
		public TabMember(int id, String text, int iconResId) {
			this(id, text, iconResId, iconResId);
		}

		public TabMember(int id, String text, int unselectedResId,
				int selectedResId) {
			this.id = id;
			this.text = text;
			this.unselectedResId = unselectedResId;
			this.selectedResId = selectedResId;
		}
	}

	public static interface OnTabClickListener {
		public abstract void onTabClick(TabMember tabMember);
	}
}
